/*
 * @filename OpenApiConfiguration.java
 *
 * @author barry
 *
 * @version 0.0.1
 *
 * @date 2020年2月14日
 */
package cn.com.lms.config;

import com.bnzj.core.util.CoreUtils;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

/**
 * 配置API文档
 *
 * @author barry
 * @date 2020-02-14
 */
@Configuration
public class OpenApiConfiguration {
    
    @Resource
    private ServerProperties serverProperties;
    
    @Bean
    public OpenAPI customOpenAPI(@Value("${springdoc.version}") String appVersion) throws UnknownHostException {
        List<Server> servers = new ArrayList<Server>();
        servers.add(new Server().url("https://localhost:" + serverProperties.getPort()).description("本地环境"));
        servers.add(new Server().url("https://back.bnzj.com.cn").description("测试环境"));
        servers.add(new Server().url("https://" + CoreUtils.getLocalIPList().get(0) + ":" + serverProperties.getPort()).description("开发环境"));
        return new OpenAPI()
            .components(new Components().addSecuritySchemes("basicScheme",
                new SecurityScheme().type(SecurityScheme.Type.APIKEY).scheme("basic")))
            .info(new Info().title("培训管理系统 API").version(appVersion)
                .license(new License().name("Private").url("http://hros.bnzj.com.cn")))
            .servers(servers);
    }


    @Bean
    public GroupedOpenApi systemOpenApi() {
        String[] paths = {"/system/**"};
        return GroupedOpenApi.builder().setGroup("系统管理").pathsToMatch(paths).build();
    }
    
    @Bean
    public GroupedOpenApi organizationOpenApi() {
        String[] paths = {"/organization/**"};
        return GroupedOpenApi.builder().setGroup("组织架构").pathsToMatch(paths).build();
    }

    @Bean
    public GroupedOpenApi examOpenApi() {
        String[] paths = {"/exam/**"};
        return GroupedOpenApi.builder().setGroup("考试管理").pathsToMatch(paths).build();
    }
    


    @Bean
    public GroupedOpenApi courseOpenApi() {
        String[] paths = {"/course/**"};
        return GroupedOpenApi.builder().setGroup("课程管理").pathsToMatch(paths).build();
    }


    @Bean
    public GroupedOpenApi cozOpenApi() {
        String[] paths = {"/coz/**"};
        return GroupedOpenApi.builder().setGroup("目录管理").pathsToMatch(paths).build();
    }

    @Bean
    public GroupedOpenApi questionnaireOpenApi() {
        String[] paths = {"/questionnaire/**"};
        return GroupedOpenApi.builder().setGroup("问卷调查").pathsToMatch(paths).build();
    }

    @Bean
    public GroupedOpenApi recMtcOpenApi() {
        String[] paths = {"/rec/**"};
        return GroupedOpenApi.builder().setGroup("员工审报").pathsToMatch(paths).build();
    }

    @Bean
    public GroupedOpenApi learningOpenApi() {
        String[] paths = {"/learning/**"};
        return GroupedOpenApi.builder().setGroup("学习计划").pathsToMatch(paths).build();
    }
    @Bean
    public GroupedOpenApi appOpenApi() {
        String[] paths = {"/app/**"};
        return GroupedOpenApi.builder().setGroup("app接口").pathsToMatch(paths).build();
    }

    @Bean
    public GroupedOpenApi reportOpenApi() {
        String[] paths = {"/report/**"};
        return GroupedOpenApi.builder().setGroup("统计报表").pathsToMatch(paths).build();
    }
}
